

    function one(){
        var xmlhttp;

        xmlhttp=new XMLHttpRequest();

        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                var json = JSON.parse(xmlhttp.responseText);
                console.log(json.data);

                json = json.data;

                for(var i = 0 ; i < json.length ; i++){

                    var li = document.createElement("li");

                    var spanName = document.createElement("span");
                    spanName.innerHTML = json[i].studentName;

                    var spanSex = document.createElement("span");
                    spanSex.innerHTML = json[i].sex;

                    var spanAge = document.createElement("span");
                    spanAge.innerHTML = json[i].age;


                    li.appendChild(spanName);
                    li.appendChild(spanSex);
                    li.appendChild(spanAge);

                    document.querySelector("#list").appendChild(li);
                    document.querySelector("#a").value = "";
                    document.querySelector("#c").value = "";
                }
            }
        };
        xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/student",true);
        xmlhttp.send();
    }
    one();





    document.querySelector("#btn").addEventListener('click',function(){

        var a = document.querySelector("#a").value;

        var b = document.querySelector("#b").value;

        var c = document.querySelector("#c").value;

        if (a == ""){
            alert("请输入正确的名字");
        }else if (c == ""){
            alert("请输入正确的年龄")
        }else {

        var xmlhttp;
        xmlhttp=new XMLHttpRequest();

        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.querySelector("#list").innerHTML = "";
                one();
            }
        };
        xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/addStudent?studentName=" + a + "&sex=" + b + "&age=" + c,true);
        xmlhttp.send();
        }
    });
